package com.ewzj.bigdata.mvp.presenter;

import android.app.Application;
import android.app.Fragment;
import android.content.Context;

import com.ewzj.bigdata.app.GlobalHttpHandlerImpl;
import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.app.utils.RxUtils;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseJson;
import com.ewzj.bigdata.mvp.model.entity.resbean.TokenBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.UserBean;
import com.google.gson.Gson;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.LoginFragmentContract;
import com.jess.arms.utils.DataHelper;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;


@ActivityScope
public class LoginFragmentPresenter extends BasePresenter<LoginFragmentContract.Model, LoginFragmentContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;
    private Gson mGson;
    private UMAuthListener authListener=new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
            loginByThird(map.get("accessToken"),map.get("openid"),share_media.name());
//            if (share_media==SHARE_MEDIA.WEIXIN){
//                loginByThird(map.get("accessToken"),map.get("openid"),share_media.name());
//            }else {
//                loginByThird(map.get("accessToken"),map.get("uid"),share_media.name());
//            }
        }

        @Override
        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
            mRootView.dismisloadingDialog();
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media, int i) {
            mRootView.dismisloadingDialog();
        }
    };
    @Inject
    public LoginFragmentPresenter(LoginFragmentContract.Model model, LoginFragmentContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager,Gson gons) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
        this.mGson=gons;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void login(String phone, String pass) {
        mModel.login(phone,pass).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<UserBean>(mErrorHandler) {
                    @Override
                    public void onNext(UserBean userBean) {
                        DataHelper.setStringSF(mRootView.getCurrentActivity(),AppConstants.APP_TOKEN,userBean.getToken());
                        DataHelper.setStringSF(mRootView.getCurrentActivity(),AppConstants.USERID,userBean.getId()+"");
                        mRootView.loginSuccess();
                    }
                });

    }

    public void initThirdLogin(SHARE_MEDIA platname){
            mRootView.showloadingDialog();
            UMShareAPI.get(mRootView.getCurrentActivity()).getPlatformInfo(mRootView.getCurrentActivity(), platname, authListener);

    }
    public void loginByThird(String token, String openid, String name){
        mModel.loginByThird(token,openid,name).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<TokenBean>(mErrorHandler) {
                    @Override
                    public void onNext(TokenBean tokenBean) {
//                        TokenBean tokenBean=mGson.fromJson(GlobalHttpHandlerImpl.decodeFromJNI(baseJson.getCipher()),TokenBean.class);
                        DataHelper.setStringSF(mRootView.getCurrentActivity(),AppConstants.APP_TOKEN,tokenBean.getToken());
                        mRootView.loginSuccess();
                    }
                });
    }
}
